package decision;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.util.ArrayList;

import android.os.Environment;
import lifeDomain.State;

public class TData {
	public State trigger;
	public int triggerIndex;
	
	public ArrayList<State[]> mStates;
	public boolean isDirty = false;
	
	public int Count()
	{
		return mStates.size();
	}
	public TData(){
		mStates = new ArrayList<State[]>();
	}
	public TData(ArrayList<State[]> states)
	{
		mStates = states;
	}
	public int compareTo(TData rp)
	{
		return trigger.compareTo(rp.trigger);
	}
	public void SetTrigger(State _trigger)
	{
		SetTrigger(_trigger, _trigger.value);
	}
	public void SetTrigger(State _trigger, int value)
	{
		trigger = _trigger;
		trigger.value = value;
		
		if(Count() == 0)
		{
			isDirty = false;
			return;
		}
		triggerIndex = -1;
		for(int i=0;i<mStates.get(0).length;i++)
		{
			if(_trigger.state == mStates.get(0)[i].state)
			{
				triggerIndex = i;
				break;
			}
		}
	}
	public int Add(State[] data, int index)
	{
		if(Count() == 0)
		{
			trigger = data[index];
		}
		
		mStates.add(data);
		return Count(); 
	}
	public void Clear()
	{
		mStates.clear();
	}
	public int GetPositive()
	{
		int i;
		int sum=0;
		
		for(i=0;i<Count();i++)
		{
			if(trigger.value == mStates.get(i)[triggerIndex].value)
			{
				sum++;
			}
		}
		return sum;
		
	}
	public ArrayList<State> GetStates()
	{
		if(Count() <= 0)
		{
			return null;
		}
		ArrayList<State> list = new ArrayList<State>();
		for(int i=0;i<mStates.get(0).length;i++)
		{
			list.add(mStates.get(0)[i]);
		}
		return list;
	}
	public ArrayList<Integer> GetValues(State state)
	{
		int index=-1;
		int i;
		
		ArrayList<Integer> list = new ArrayList<Integer>();
		
		for(i=0;i<mStates.get(0).length;i++)
		{
			if(mStates.get(0)[i].state == state.state)
			{
				index = i;
				break;
			}
		}
		if(index == -1)
			return null;
		
		for(i=0;i<Count();i++)
		{
			if(!list.contains(mStates.get(i)[index].value))
			{
				list.add(mStates.get(i)[index].value);
			}
		}
		return list;
	}
	public void Add(State[] states)
	{
		mStates.add(states);
		
	}
	
	public TData GetSubsetWithValue(State state, int value)
	{
		int i, j, index=-1;
		TData sub = new TData();
		
		for(i=0;i<mStates.get(0).length;i++)
		{
			if(mStates.get(0)[i].state == state.state)
			{
				index = i;
				break;
			}
		}
		if(index == -1)
			return null;
		for(i=0;i<Count(); i++)
		{
			if(mStates.get(i)[index].value != value)
				continue;
			State[] temp = mStates.get(i);
			State[] res = new State[temp.length-1];
			int cnt=0;
			for(j=0;j<temp.length;j++)
			{
				if(j == index)
					continue;
				
				res[cnt] = temp[j];
				cnt++;
			}
			sub.Add(res);
		}
		sub.SetTrigger(this.trigger);
		return sub;
	}
	public void GetFileName(String prefix)
	{
		
	}
	
	
	
}















